//
//  NetTool.m
//  Jyxt
//
//  Created by 刘日 on 17/5/25.
//  Copyright © 2017年 DaLian Rental Housing Management. All rights reserved.
//

#import "NetTool.h"
#import <AFNetworking.h>
#import <MJExtension.h>


@implementation NetTool

/**
 Get请求
 
 @param url 要请求的url
 @param data 请求的数据对象
 @param onSuccess 请求成功的回调block
 @param onError 请求失败的回调block
 */
+ (void)getWithUrl: (NSString *)url data: (NSObject *)data onSuccess: (REQUEST_SUCCESS)onSuccess onError: (REQUEST_ERROR)onError{
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    [sessionManager.requestSerializer setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    [sessionManager GET: url parameters: data progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        onSuccess(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        onError(error);
    }];
}

/**
 Post请求
 
 @param url 要请求的url
 @param data 请求的数据对象
 @param onSuccess 请求成功的回调block
 @param onError 请求失败的回调block
 */
+ (void)postWithUrl: (NSString *)url data: (NSObject *)data onSuccess: (REQUEST_SUCCESS)onSuccess onError: (REQUEST_ERROR)onError{
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    [sessionManager POST: url parameters: data progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        onSuccess(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        onError(error);
    }];
}

/**
 Post请求
 
 @param url 要请求的url
 @param data 请求的数据对象
 @param onSuccess 请求成功的回调block
 @param onError 请求失败的回调block
 */
+ (void)postImageWithUrl: (NSString *)url data: (NSObject *)data onSuccess: (REQUEST_SUCCESS)onSuccess onError: (REQUEST_ERROR)onError{
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.responseSerializer = [AFImageResponseSerializer serializer];
    [sessionManager POST: url parameters: data progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        onSuccess(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        onError(error);
    }];
    
    
}

+ (void)uploadWithUrl: (NSString *)url parameters:(NSObject *)parameters data: (NSDictionary<NSString *, UIImage *> *)data onSuccess: (REQUEST_SUCCESS)onSuccess onError: (REQUEST_ERROR)onError{
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    if (parameters == nil) {
        parameters = @{};
    }
    [sessionManager POST: url parameters: parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        for (NSString *key in data.allKeys)
            [formData appendPartWithFileData: UIImagePNGRepresentation(data[key]) name: @"files" fileName: [NSString stringWithFormat: @"hello-%lu.png" , (unsigned long)[key hash]] mimeType: @"image/png"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        onSuccess(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        onError(error);
    }];
}

+ (NSError *)dealServerResponseError: (NSDictionary *)responseObj{
    if (responseObj[@"success"] && [responseObj[@"success"] intValue] == 0){
        // success 0
        return [NSError errorWithDomain: NSNetServicesErrorDomain code: 777 userInfo: @{@"message": @"服务器内部异常"}];
    }
    else if ([responseObj[@"status"] isEqualToString: @"failure"]) {
        // 业务接口返回错误
        if (responseObj[@"data"][@"code"]) {
            return [NSError errorWithDomain:NSNetServicesErrorDomain code:666 userInfo:@{@"message" : @"用户名或密码错误"}];
        }
        return [NSError errorWithDomain: NSNetServicesErrorDomain code: 888 userInfo: @{@"message":responseObj[@"errors"][0][@"errmsg"]}];
    }
    else{
        return [NSError errorWithDomain: NSNetServicesErrorDomain code: 1000 userInfo: @{@"message": @"应用关键性终止错误"}];
    }
}

@end
